package com.hsh.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsh.erp.domain.common.Result;
import com.hsh.erp.domain.dto.ShopDTO;
import com.hsh.erp.domain.entity.Shop;
import com.hsh.erp.domain.vo.ShopVO;
import com.hsh.erp.init.DataInitializer;
import com.hsh.erp.mapper.ShopMapper;
import com.hsh.erp.service.ShopService;
import com.hsh.erp.utils.MyBeanUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author USER
 * @description 针对表【shop】的数据库操作Service实现
 * @createDate 2024-05-30 16:21:43
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop>
        implements ShopService {

    @Resource
    private DataInitializer dataInitializer;
    /**
     * 获取所有店铺
     *
     * @return
     */
    @Override
    public Result<List<ShopDTO>> getAll() {
        List<ShopDTO> shopDTOList = DataInitializer.getShopDTOList();
        if (shopDTOList.isEmpty()){
            return getList(new ShopVO().setIsDelete(0));
        }
        return Result.success(shopDTOList);
    }

    /**
     * 列表查询
     *
     * @param shopVO
     * @return
     */
    @Override
    public Result<List<ShopDTO>> getList(ShopVO shopVO) {
        shopVO.selectInit();
        Wrapper wrapper = new QueryWrapper()
                .like(Strings.isNotBlank(shopVO.getName()), "name", shopVO.getName())
                .eq(shopVO.getIsDelete() != null, "is_delete", shopVO.getIsDelete());
        List<ShopDTO> shopDTOList
                = MyBeanUtils.copyList(list(wrapper), ShopDTO.class);
        return Result.success(shopDTOList);
    }

    /**
     * 修改一条记录
     *
     * @param shopVO
     * @return
     */
    @Override
    public Result updateOne(ShopVO shopVO) {
        boolean update = updateById(MyBeanUtils.copy(shopVO, Shop.class));
        dataInitializer.initShop();
        return Result.create(update);
    }

    /**
     * 删除一条记录
     *
     * @param shopVO
     * @return
     */
    @Override
    public Result deleteOne(ShopVO shopVO) {
        return updateOne(shopVO);
    }


    /**
     * 新增一条记录
     *
     * @param shopVO
     * @return
     */
    @Override
    public Result addOne(ShopVO shopVO) {
        boolean save = save(MyBeanUtils.copy(shopVO, Shop.class));
        dataInitializer.initShop();
        return Result.create(save);
    }

    /**
     * 根据id获取名称
     *
     * @param id
     * @return
     */
    @Override
    public String getNameById(Integer id) {
        List<Shop> shopList = list(new QueryWrapper<Shop>().eq("is_delete", 0));
        // 获得商品map，key为id，value为名称
        Map<Integer, String> shoptMap = shopList
                .stream().collect(Collectors.toMap(Shop::getId, Shop::getName));
        String name;
        if (id != null && shoptMap.containsKey(id)) {
            name = shoptMap.get(id);
        } else {
            name = "";
        }
        return name;
    }
    //============================================私有方法开始============================================
    //============================================私有方法结束============================================
}




